﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLayer;

namespace PayRoll
{
    public partial class ChangePassword : Form
    {
          
        private string ErrMsg = "New password not match";
        private int intEmployeeID = 0;
        
        public ChangePassword()
        {
            InitializeComponent();
            
        }

        public ChangePassword(int intSetEmployeeID, bool blCalledbuUser)
        {
            InitializeComponent();
            intEmployeeID = intSetEmployeeID;
            if (blCalledbuUser)
                btnCancel.Visible = true;
            
        }
             
        private void btnOk_Click(object sender, EventArgs e)
        {
            if ((txtOldPws.Text).Length == 0 || (txtPassword.Text).Length == 0 || (txtRePassword.Text).Length == 0)
            {
                MessageBox.Show("Fealds are empty", "Error Message!");
            }
            else
            {
                ClsEmployee objEmployee = new ClsEmployee();
                objEmployee.GetEmployee(Utility.LoginEmployeeID);
                if (objEmployee.PWD == txtOldPws.Text)
                {
                    bool blStatus = CheckValidPassowrd(txtPassword.Text.Trim(), txtRePassword.Text.Trim());
                    if (blStatus)
                    {
                        string newPWD = txtPassword.Text;
                        int employeeID = objEmployee.EmployeeID;
                        ClsChangePassword objChangePWD = new ClsChangePassword();
                        objChangePWD.SaveEmploeePWD(employeeID, newPWD);
                        this.Close();
                        MessageBox.Show("Password change Successfully");
                    }
                    if (txtPassword.Text != txtRePassword.Text)
                    {
                        MessageBox.Show(ErrMsg, "Error Message!");

                    }
                }
               
                else
                {   
                    MessageBox.Show("Old password not match", "Error Message!");
                }
            }

            try
            {         
                
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            
        }

        private bool CheckValidPassowrd(string strPassoword, string strRePassoword)
        {

            if (strPassoword.Equals(strRePassoword.ToString()))
            {
                if (strPassoword.Length >= 5)
                {
                    ClsLogin objClsLogin = new ClsLogin();
                    objClsLogin.UpdatePasswordChangedStatus(intEmployeeID);
                    return true;
                }
                else
                    MessageBox.Show("New password minimum 5 character");
                return false;
               
            }
            else return false;

                                   
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChangePassword_Load(object sender, EventArgs e)
        {

        }
     

    }
}
